package top.hidoctor.auscultation.feature.fragment.mine;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.tang.net.NetClient;
import com.tang.user.UserInfo;
import com.tang.user.UserInfoManager;
import com.tang.util.DialogUtil;
import com.tang.util.GsonUtil;
import com.tang.util.SPUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.base.BaseFragment;
import top.hidoctor.auscultation.base.Constant;
import top.hidoctor.auscultation.event.UserEvent;
import top.hidoctor.auscultation.feature.activity.FeedBackActivity;
import top.hidoctor.auscultation.feature.activity.LocalHistoryActivity;
import top.hidoctor.auscultation.feature.activity.LoginActivity;
import top.hidoctor.auscultation.feature.activity.SettingActivity;
import top.hidoctor.auscultation.feature.activity.StatisticsActivity;
import top.hidoctor.auscultation.feature.activity.TestActivity;
import top.hidoctor.auscultation.feature.activity.TestActivity2;
import top.hidoctor.auscultation.widget.GlideCircleTransform;
import top.hidoctor.auscultation.widget.roundimageview.RoundedImageView;

public class MineFragment extends BaseFragment implements View.OnClickListener {

    private ImageButton ibSetting;
    private TextView tvFeedback, tvStatistics;
    private Button btnGoout;
    private RoundedImageView ivAvater;
    private TextView tvAvater;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        initView();
    }

    private void initView() {
        tvAvater = getActivity().findViewById(R.id.tv_avater);
        ivAvater = getActivity().findViewById(R.id.iv_avaters);
        ibSetting = getActivity().findViewById(R.id.ib_setting);
        tvFeedback = getActivity().findViewById(R.id.tv_feedback);
        btnGoout = getActivity().findViewById(R.id.btn_goout);
        tvStatistics = getActivity().findViewById(R.id.tv_statistics);
        ibSetting.setOnClickListener(this);
        tvFeedback.setOnClickListener(this);
        tvStatistics.setOnClickListener(this);
        btnGoout.setOnClickListener(this);

        initUser();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ib_setting:
                startActivity(new Intent(getContext(), SettingActivity.class));
                break;
            case R.id.tv_feedback:
                startActivity(new Intent(getContext(), FeedBackActivity.class));
                break;
            case R.id.tv_statistics:
                startActivity(new Intent(getContext(), StatisticsActivity.class));
                break;
            case R.id.btn_goout:
                showAlertDialog("", "确定要退出?", new DialogUtil.DialogButtonClickListener() {
                    @Override
                    public void onPositiveButtonClick(DialogInterface dialog) {
                        dialog.dismiss();
                        UserInfoManager.getInstance().clearData();
                        Intent intent = new Intent(getContext(), LoginActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        getActivity().finish();
                    }

                    @Override
                    public void onNegativeButtonClick(DialogInterface dialog) {
                        dialog.dismiss();
                    }
                });

                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(UserEvent event) {
        initUser();
    }

    private void initUser() {
        UserInfo userInfo = UserInfoManager.getInstance().getUserInfo();
        if (userInfo != null) {
            RequestOptions optionsCircle = new RequestOptions();
            DrawableTransitionOptions transitionOptions = new DrawableTransitionOptions();
            Glide.with(mContext)
                    .load(NetClient.IMG_URL + userInfo.getAvatar())
                    .apply(optionsCircle.transform(new GlideCircleTransform()).placeholder(R.drawable.ic_avater_zw))
                    .transition(transitionOptions.crossFade())
                    .into(ivAvater);
            tvAvater.setText(userInfo.getRealName());
        }
    }
}
